﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using ZK.Entity;

namespace ZK.Pack
{
    public class TcpCIJPrinter : TcpLongPrinter,IPrinter
    {

        public TcpCIJPrinter(string ip,int portName) : base(ip,portName)
        {


        }


        private object syncLocal = new object();
        public Task task;
        public Queue<string> queue = new Queue<string>();

        CancellationTokenSource tokenSource ;

        public event EventHandler EventHandlerPrinted;

        private void ExcuteTask()
        {
            while (!tokenSource.IsCancellationRequested)
            {
                lock (syncLocal)
                {
                    if (queue.Count > 0)
                    {
                        base.WriteData(queue.Dequeue());
                        //byte[] senddata = Encoding.ASCII.GetBytes(queue.Dequeue());
                        //base.WriteData(senddata);
                    }
                }
                Thread.Sleep(1);
            }

        }

        public override void WriteData(string data)
        {
            lock (syncLocal)
            {
                queue.Enqueue(data);
            }

        }

        //打开并启动线程
        public override void Open()
        {
            base.Open();
            tokenSource = new CancellationTokenSource();
            task = new Task(ExcuteTask, tokenSource.Token);
            task.Start();
        }

        public override void Close()
        {
            try
            {
                base.IsOpen = false;
                if (tokenSource != null)
                    tokenSource.Cancel();
                Thread.Sleep(100);
                base.Close();
            }
            catch { };
        }


        public override void TcpDataReceived(object sender)
        {
            try
            {
                if (!base.IsOpen) return;
                base.EventReceived(sender);
                //byte[] result = (byte[])sender;


                //if (result[3] == 0)
                //{
                //    base.EventReceived("OK");
                //}
                //else {
                //    base.EventReceived("NG");
                //}
                //char chr = (char)0;
                //if (result.Length > 0)
                //{
                //    chr = (char)result[0];
                //}
                //if (chr == 6)
                //{
                //    base.EventReceived("P");

                //}
                //else if (chr == (char)'!')
                //{
                //    base.EventReceived("NG");
                //}
                //else {
                //    base.EventReceived("OK");
                //}
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
        }

        public void WriteData(SendData sendData)
        {
            
        }

        public string BuildPrintData(SendData sendData)
        {

            StringBuilder stringBuilder = new StringBuilder(512);
            byte[] stx = new byte[] { 2 };
            byte[] etx = new byte[] { 3 };
            byte[] sep = new byte[] { 10 }; //0Ah
            char type = 'U';

            stringBuilder.Append(Encoding.ASCII.GetString(stx));
            stringBuilder.Append(type);
            stringBuilder.Append(sendData.Command); //
            stringBuilder.Append(Encoding.ASCII.GetString(sep));
            stringBuilder.Append(sendData.Msg);
            stringBuilder.Append(Encoding.ASCII.GetString(etx));
            return stringBuilder.ToString();
        }

        public void WriteDataOnce(SendData sendData  )
        {
          
        }
    }
}
